package com.tys.algorithm.advanced.test.class03;

import java.util.Stack;

/**
 * 获得栈中最小值
 * 方法1：最小栈和数据栈不同步增长
 * 方法2：最小栈和数据栈，同步增长
 */
public class Code05_GetMinStack {
    public static class MyStack1 {
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public MyStack1() {
            stackData = new Stack<>();
            stackMin = new Stack<>();
        }

        public void push(int value) {
            if (stackMin.isEmpty()) {
                stackMin.push(value);
            } else if (value <= getmin()) {
                stackMin.push(value);
            }
            stackData.push(value);
        }

        public int pop() {
            if (stackData.isEmpty()) {
                throw new RuntimeException("empty");
            }
            int value = stackData.pop();
            if (value == getmin()) {
                stackMin.pop();
            }
            return value;
        }

        public boolean isEmpty() {
            return stackData.isEmpty();
        }

        public int getmin() {
            if (stackMin.isEmpty()) {
                throw new RuntimeException("empty");
            }
            return stackMin.peek();
        }
    }

    public static class MyStack2 {
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public MyStack2() {
            stackData = new Stack<>();
            stackMin = new Stack<>();
        }

        public void push(int value) {
            if (stackMin.isEmpty()) {
                stackMin.push(value);
            } else if (value < getmin()) {
                stackMin.push(value);
            } else {
                stackMin.push(getmin());
            }
            stackData.push(value);
        }

        public int pop() {
            if (stackData.isEmpty()) {
                throw new RuntimeException("empty");
            }
            stackMin.pop();
            return stackData.pop();
        }

        public boolean isEmpty() {
            return stackData.isEmpty();
        }

        public int getmin() {
            if (stackMin.isEmpty()) {
                throw new RuntimeException("empty");
            }
            return stackMin.peek();
        }
    }

    public static void main(String[] args) {
//        Stack stack = new Stack();
//        System.out.println(stack.peek());
        MyStack1 stack1 = new MyStack1();
        stack1.push(3);
        System.out.println(3 == stack1.getmin());
        stack1.push(3);
        System.out.println(3 == stack1.getmin());
        stack1.push(4);
        System.out.println(3 == stack1.getmin());
        stack1.push(1);
        System.out.println(1 == stack1.getmin());
        stack1.push(1);
        System.out.println(1 == stack1.getmin());
        System.out.println(1 == stack1.pop());
        System.out.println(1 == stack1.getmin());
        System.out.println("================");

        MyStack2 stack2 = new MyStack2();
        stack2.push(3);
        System.out.println(3 == stack2.getmin());
        stack2.push(4);
        System.out.println(3 == stack2.getmin());
        stack2.push(1);
        System.out.println(1 == stack2.getmin());
        stack2.push(1);
        System.out.println(1 == stack2.getmin());
        System.out.println(1 == stack2.pop());
        System.out.println(1 == stack2.getmin());

    }

}
